<?php
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;
require_once("./vendor/autoload.php");
require_once("./lib/fun.php");
require_once("./lib/pdo.php");
//微信支付参数
$wx_config =  [
            'miniapp_id' => 'wxfd7508b055adc597',
            'mch_id' => '1656795437',
            'key' => 'q4CpKGGxmqkxPWjGKrZlFVXpWTBDA3i7',
            'notify_url' => 'https://www.uluda.cn/pay.php',
            'log' => [ // optional
                'file' => 'pay.log',
                'level' => 'info',
                'type' => 'single',
                'max_file' => 30,
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
            ],
        ];
//vip数据
$db_vip = db('lu_vip');

$_vip = $db_vip->select();
$param = I("param.");
$act = I("param.act",'wx_back');
//小程序支付
if($param['act'] == 'wx_miniapp'){
    //获取用户d
    
    $rule  = [
        'id'  => [
            'type'         => 'number',
            'is_necessary' => 1,
            'name' => '订单ID',
        ],
        'act'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'act',
        ],
         'token'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'token',
        ],
    ];
    $check = CheckParams($param,$rule);
    if(!$check['success']){
        $msg = explode(";",$check['msg']);
        json([
            'code' => 0,
            'msg'  => $msg[0] ?? "",
        ]);
    }

    $db_user = db('lu_user');
    $token = str_replace(" ","+",I("token"));
    $uid = authcode($token,'DECODE',"youluda",7*24*3600);
    $user_info = $db_user->where(['id' => $uid])->find();
    if(empty($user_info['openid'])){
         json([
            'code' => 0,
            'msg' => '获取openid失败',
        ]);
    }
    $db = db("lu_order");
    $vip_data = $_vip;
    $vip_data = returnArrByKey($_vip,'id');
    if(!isset($vip_data[$param['id']])){
        json([
            'code' => 0,
            'msg'  => '参数错误',
        ]);
    }
    $vip_data = $vip_data[$param['id']];
    $order_no =  makeOrderNum();
    // $vip_data['money'] = 0.01;
    $add_order = [
            'vip_id' => $param['id'],
            'order_sn' => $order_no,
            'price' => $vip_data['money'],
            'type' => 1,
            'create_time' => time(),
            'uid' => $user_info['id'],
            ];
    $id = $db->add($add_order);
    if($id){
        $order = [
                'out_trade_no' => $order_no,
                'total_fee' =>  $vip_data['money'] * 100, // **单位：分**
                'body' => '用户充值',
                'openid' => $user_info['openid'],
            ];
        $res  = Pay::wechat($wx_config)->miniapp($order);
        json([
                'code' => 1,
                'info' => $res,
            ]);
    }else{
        json([
            'code' => 0,
            'msg'  => '提交失败',
        ]);
    }
    
}
//小程序回写
if($act == 'wx_back'){
    $pay = Pay::wechat($wx_config);
    file_put_contents("./wx.txt",PHP_EOL."<------------>".date("Y-m-d H:i:s").'<---->'.PHP_EOL,FILE_APPEND);
    $data = $pay->verify();
    $info = $data->all();
    $info_arr = json_decode(json_encode($info), true);
    file_put_contents("./wx.txt",json_encode($info_arr),FILE_APPEND);
    $db = db('lu_order');
    $where = [
            'order_sn' => $info_arr['out_trade_no'],
        ];
    $order_info = $db->where($where)->find();
    //成功
    if($info_arr['result_code'] == 'SUCCESS'){
        // if($order_info['price'] * 100 == $info_arr['total_fee'] && $order_info['status'] == 1){
        if($order_info['status'] == 1){
            $vip_id = $order_info['vip_id'];
            $vip_data = returnArrByKey($_vip,'id');
            $vip_time = $vip_data[$vip_id]['month'] * 30 * 24 * 3600;
            
            $vip_info = $vip_data[$vip_id];
            //更新账单信息
            $db->where($where)->where($where)->update([
                    'status' => 2,
                ]);
            //更新用户信息
            $where = [
                'id' => $order_info['uid'],
                ];
            $db_user = db("lu_user");
            $user_info = $db_user->where($where)->find();
            
            //普通会员
            if($vip_info['type'] == 0){
                if($user_info['vip_time'] < time()){
                    $update = [
                        'vip_time' => time() + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'type' => $vip_info['type'] ?? 0,
                        
                        ];
                    $res = $db_user->where($where)->update($update);
                }else{
                    $update = [
                        'vip_time' => $user_info['vip_time'] + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'type' => $vip_info['type'] ?? 0,
    
                        ];
                    $res = $db_user->where($where)->update($update);
                }
            }else if($vip_info['type'] == 1){
                if($user_info['business_time'] < time()){
                    $update = [
                        'business_time' => time() + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'business_user_limit' => $vip_info['business_user_limit'],
                        'business_share_limit' => $vip_info['business_share_limit'],
                        'type' => $vip_info['type'] ?? 0,
                        
                        ];
                    $res = $db_user->where($where)->update($update);
                }else{
                    $update = [
                        'business_time' => $user_info['business_time'] + $vip_time,
                        'vip_id' => $order_info['vip_id'],
                        'limit_plan_num' => $vip_info['plan_num'],
                        'limit_address_num' => $vip_info['address_num'],
                        'business_user_limit' => $vip_info['business_user_limit'],
                        'business_share_limit' => $vip_info['business_share_limit'],
                        'type' => $vip_info['type'] ?? 0,
    
                        ];
                    $res = $db_user->where($where)->update($update);
                }
            }else if($vip_info['type'] == 2){//购买面单
                $update = [
                    'img_num' => $user_info['img_num'] + $vip_info['num'],
                    ];
                $res = $db_user->where($where)->update($update);
                
                
            }else if($vip_info['type'] == 3){//短信
                $update = [
                    'msg_num' => $user_info['msg_num'] + $vip_info['num'],
                    ];
                $res = $db_user->where($where)->update($update);
            }
            
            file_put_contents("./wx.txt",PHP_EOL."res:<-------->".$res.PHP_EOL,FILE_APPEND);
            
        }
        
        
        
    }

}

?>